

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ChessNet {
	public static final int PORT = 9999;

	private BufferedReader in;

	private PrintWriter out;

	public ChessNet() {

	}

	public void startListen() {
		Thread t = new Thread(){
			@Override
			public void run() {
				try {
					ServerSocket ss = new ServerSocket(PORT);
					Socket s = ss.accept();
					in = new BufferedReader(new InputStreamReader(s.getInputStream()));
					out = new PrintWriter(s.getOutputStream(), true);
					startReadThread();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		};
		t.setDaemon(true);
		t.start();
	}
	private void startReadThread(){
		Thread t = new Thread(){
			public void run() {
				while(true){
					try {
						String line = in.readLine();
						parseLine(line);
					} catch (IOException e) {
						e.printStackTrace();
					}
					
					
				}
			}
		};
		t.start();
	}
	protected void parseLine(String line) {
		if(line.startsWith("��:")){
			String s = line.substring(2);
			String[] a = s.split(",");
			int row = Integer.parseInt(a[0]);
			int col = Integer.parseInt(a[1]);
			Constants.cont.putChessByOther(row,col);
		}else if(line.startsWith("��:")){
//			Constants.cont.chat(line);
		}
	}

	public void startLink(String ip) {
		try {
			Socket s = new Socket(ip,PORT);
			in = new BufferedReader(new InputStreamReader(s.getInputStream()));
			out = new PrintWriter(s.getOutputStream(), true);
			startReadThread();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void sendChessPoint(int row, int col) {
		out.println("��:" + row + "," + col);
	}

}
